package com.cskaoyan.service;

import com.cskaoyan.bean.bo.KeywordCreateBo;
import com.cskaoyan.bean.entity.Keyword;
import com.cskaoyan.bean.entity.KeywordExample;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.BaseParam;
import com.cskaoyan.bean.vo.WxSearchVo;
import com.cskaoyan.common.util.ObjectUtils;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.utils.MarketPageUtil;
import com.cskaoyan.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author break_a_leg
 * @description
 * @date 2021年10月19日 15:17
 */
@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public BasePageData list(String keyword, String url, BaseParam param) {
        // 配置分页
        MarketPageUtil.startPage(param);

        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (StringUtils.isNotEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (StringUtils.isNotEmpty(url)) {
            criteria.andUrlLike("%" + url + "%");
        }

        // 设置排序
        keywordExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        return BasePageData.ok(keywords);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Keyword create(KeywordCreateBo keywordCreateBo) {
        Keyword keyword = new Keyword();
        keyword.setKeyword(keywordCreateBo.getKeyword());
        keyword.setUrl(keywordCreateBo.getUrl());
        keyword.setIsHot(keywordCreateBo.getIsHot());
        keyword.setIsDefault(keywordCreateBo.getIsDefault());
        keywordMapper.insert(keyword);
        Keyword data = keywordMapper.selectByUseGeneratedKeys(keyword.getId());
        return data;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Keyword update(Keyword keyword) {
        keywordMapper.updateByPrimaryKey(keyword);
        Keyword data = keywordMapper.selectByPrimaryKey(keyword.getId());
        return data;
    }

    @Override
    public int delete(Keyword keyword) {
        int affectedRows = keywordMapper.loginDeleteByPrimaryKey(keyword);
        return affectedRows;
    }

    /**
     * <h2> 小程序端 关键词检索助手 </h2>
     */
    @Override
    public List helper(String keyword) {

        /**
         * 创建 添加条件
         * 使用条件查询
         */
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        if (StringUtils.isNotEmpty(keyword)) criteria.andKeywordLike("%" + keyword + "%");
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        List<String> keywordList = new LinkedList<>();
        for (Keyword kw : keywords) {
            String keyword2 = kw.getKeyword();
            keywordList.add(keyword2);
        }
        return keywordList;
    }

    /**
     * <h2> 小程序端 搜索首页 </h2>
     */
    @Override
    public WxSearchVo index() {
        KeywordExample keywordExample = new KeywordExample();
        /***
         * 先从数据库中 传入对应的值 然后查询
         * - 处理）之后给 WxSearchVo的属性
         *TODO 赋值
         * - defaultKeyword
         * - hotKeywordList
         * - historyKeywordList 默认为空 []
         */
        List<Keyword> keywordList = keywordMapper.selectByExample(keywordExample);
        if (ObjectUtils.isNotEmpty(keywordList)) {}

        /**
         *TODO 根据这两个字段进行判断
         * isDefault Boolean 只有一个可以为 true
         * isHot Boolean 多个
         */

        return null;
    }

    /**
     * <h2> 小程序端 清空历史记录 </h2>
     */
    @Override
    public Boolean clearHistory(Map map) {

        // TODO
        return null;
    }
}
